package com.artnology.independence.views.operations;

import java.util.Collection;

import org.eclipse.core.commands.ExecutionException;
import org.eclipse.core.commands.operations.AbstractOperation;
import org.eclipse.core.runtime.IAdaptable;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;

import com.artnology.independence.model.DVNode;
import com.artnology.independence.model.DependencyGraph;

public class AddNodeToGraphOperation extends AbstractOperation {

	DependencyGraph depGraph = null;
	Collection<DVNode> nodesToAdd = null;

	public AddNodeToGraphOperation(String label, DependencyGraph depGraph,
			Collection<DVNode> nodesToAdd) {
		super(label);
		this.nodesToAdd = nodesToAdd;
		this.depGraph = depGraph;
	}

	@Override
	public IStatus execute(IProgressMonitor arg0, IAdaptable arg1)
			throws ExecutionException {
		depGraph.addNodesWithReferences(nodesToAdd);
		return Status.OK_STATUS;
		
	}

	@Override
	public IStatus redo(IProgressMonitor arg0, IAdaptable arg1)
			throws ExecutionException {
		return execute(arg0, arg1);
	}

	@Override
	public IStatus undo(IProgressMonitor arg0, IAdaptable arg1)
			throws ExecutionException {

		for (DVNode node : nodesToAdd) {
			depGraph.getGraph().removeVertex(node);
		}
		depGraph.setGraph(depGraph.getGraph());
		return Status.OK_STATUS;
	}

}
